package com.dragonlance.net;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import com.dragonlance.DLClient;
import com.dragonlance.DLServer;

public class DLMessageHandler extends IoHandlerAdapter
{
	public DLMessageHandler() {
		super();
	}
    @Override
    public void exceptionCaught( IoSession session, Throwable cause ) throws Exception
    {
        cause.printStackTrace();
    }

    @Override
    public void messageReceived(IoSession session, Object message ) throws Exception
    {
    	System.out.println("received message");
        PackageIn pkg = (PackageIn) message;
        DLServer.getInstance().getPkgInHandler().handle(pkg,session);
    }
    
    @Override
    public void sessionClosed(IoSession session) throws Exception {
    	DLClient client = DLServer.getInstance().getClientMgr().remove(session.getId());
    	if(client != null)
		{
    		client.exit();
		}
    }

    @Override
    public void sessionIdle( IoSession session, IdleStatus status ) throws Exception
    {
        System.out.println( "IDLE " + session.getIdleCount( status ));
    }
}